package utils;

import com.ulisesbocchio.jasyptspringboot.encryptor.SimplePBEStringEncryptor;
import java.util.LinkedHashMap;
import java.util.Map;
import lombok.extern.slf4j.Slf4j;
import org.cloud.utils.AES128Util;
import org.jasypt.encryption.StringEncryptor;
import org.jasypt.encryption.pbe.StandardPBEByteEncryptor;
import org.jasypt.properties.PropertyValueEncryptionUtils;
import org.junit.Assert;
import org.junit.Test;
import org.springframework.util.ObjectUtils;


@Slf4j
public class JasyptLocalTest {

    final String encKey = "xe7MfU32hfK6jTXG";
    final String aesKey = "";
    final String aesIV = "";

    final String MD5_SALT = "";
    final String HMAC_KEY = "";
    final String HMAC_SALT = "";
    final String FIN_validationSalt = "";

    @Test
    public void encryptTest() throws Exception {
        pbeByteEncryptor.setPassword(encKey);  // 更改这个密码
        Map<String, String> passwordMap = new LinkedHashMap<>();
        passwordMap.put("system.user.password.salt", "Xv8pJBpT2aigMPHe");

//        passwordMap.put("tel_bot_认证_name：", "scp_validator_bot");
//        passwordMap.put("tel_bot_认证_key：", "7589915459:AAFgrmgWMJ4uB2DFra11bEBWfWlZjmKaClY");
//        passwordMap.put("tel_bot_认证_群ID：", "-4514884752");
//        passwordMap.put("mysql用户名：", "root");
//        passwordMap.put("mysql密码：", "gSg0v4UK4Al1sU1B");
//        passwordMap.put("redis密码：", "i2uG1hI4T84eFdZh");
//        passwordMap.put("mongodb用户名：", "admin");
//        passwordMap.put("mongodb密码：", "3PzhOlQlJ9vxa3luH0fz");
//        passwordMap.put("aesKey：", aesKey);
//        passwordMap.put("aesIV：", aesIV);
//        passwordMap.put("md5 salt", MD5_SALT);
//        passwordMap.put("system.hash.hmac.key", HMAC_KEY);
//        passwordMap.put("system.hash.hmac.salt", HMAC_SALT);
//        passwordMap.put("system.finan.validationSalt", FIN_validationSalt);
        for (String key : passwordMap.keySet()) {
            String encryptStr = encrypt(passwordMap.get(key));
            Assert.assertEquals(passwordMap.get(key), decrypt(encryptStr));
            log.info("{}\tENC(\"{}\")", key, encryptStr);
        }
        // 双重加密
//        Map<String, String> passwordMapAesAndEnc = new LinkedHashMap<>();
//        passwordMapAesAndEnc.put("withdraw.private-key", "f97588dc8dcf3801d60fa11ca5d31534437fda1da285089a9f255a0ab534e78a");
//
//
//
//        passwordMap.putAll(encryptAesAndEnactTest(passwordMapAesAndEnc));
    }

    //    @Test
    public Map<String, String> encryptAesAndEnactTest(Map<String, String> passwordMapAesAndEnc) throws Exception {
        for (String key : passwordMapAesAndEnc.keySet()) {
            String encryptStr = AES128Util.single().encrypt(passwordMapAesAndEnc.get(key), aesKey, aesIV);
            encryptStr = encrypt(encryptStr);
            Assert.assertEquals(passwordMapAesAndEnc.get(key), AES128Util.single().decrypt(decrypt(encryptStr), aesKey, aesIV));
            log.info("{}\tENC(\"{}\")", key, encryptStr);
        }
        return passwordMapAesAndEnc;
    }

    //        key: ENC("O0UHnChcaGCxPkarzWRS/B8yBsbVUvazKJZ1xdJz3ZLSWn7NbSbW97qqlc53+9A4FIreyUaFkENEFBnTZRmBMQ==") #用于加密用，不要轻易改变，配置到配置中心需要进行加密
//    vi: ENC("paPZk+Mc+Gz7xKNXLRXlIIFWArOtJGtsJXKwLE7c3VC1e3iJvS7cDEopxAZBohjPC3wZmLaiuX0cvbebpyhU1A==") #用于加密用，不要轻易改变，配置到配置中心需要进行加密
    @Test
    public void decryptTest() throws Exception {
        pbeByteEncryptor.setPassword(encKey);  // 更改这个密码
        Map<String, String> passwordMap = new LinkedHashMap<>();
//        passwordMap.put("salt", "x+uKTYYplTkTCuZOECohYYwPmPD14OxfuV7VqloLz2vU+rqajlGNQui2BxtJSkqoxfJlSFuWLqR2AoptBQjtLw==");
        passwordMap.put("aes key", "tkHcr1YkgM3WKhCcIUA2RAMu+xaSLrXkxTlYbrk6BGCf7W3Ltg0dkC1BAyYxlhQvacOpfX8bHtaVBcdMGxkaVTOHOPA9y+2iTAeGtGr/da4=");
//        passwordMap.put("aes vi", "paPZk+Mc+Gz7xKNXLRXlIIFWArOtJGtsJXKwLE7c3VC1e3iJvS7cDEopxAZBohjPC3wZmLaiuX0cvbebpyhU1A==");
//        passwordMap.put("邮箱密码","G85794X6IOzGOVDgYgKMi6GVlZOdutIt9WlZ+KfTPfWZTEcpGSQxaYy6eYxLasmk");
//        passwordMap.put("mongo","");
//        passwordMap.put("salt","");
        for (String key : passwordMap.keySet()) {
            String encryptStr = decrypt(passwordMap.get(key));
            log.info("{}\t{}", key, encryptStr);
        }
    }


    @Test
    public void decryptTestAesAndEnc() throws Exception {
        if (!ObjectUtils.isEmpty(encKey)) {
            pbeByteEncryptor.setPassword(encKey);  // 更改这个密码
        }
        Map<String, String> passwordMap = new LinkedHashMap<>();
//        passwordMap.put("harvest-private-key", "");
        passwordMap.put("合约签名私钥", "653553623cacd6dd7d0fd433cef1bc5cb6cef7d2faafd7e78f6ae36bbcc1d61b");

        for (String key : passwordMap.keySet()) {
            String encryptStr = passwordMap.get(key);
            if (!ObjectUtils.isEmpty(encKey)) {
                encryptStr = decrypt(encryptStr);
            }
            encryptStr = AES128Util.single().decrypt(encryptStr, aesKey, aesIV);
            log.info("{}\t{}", key, encryptStr);
        }
    }

    private final StringEncryptor basicTextEncryptor;

    final static StandardPBEByteEncryptor pbeByteEncryptor = new StandardPBEByteEncryptor();

    static {
        pbeByteEncryptor.setAlgorithm("PBEWITHHMACSHA512ANDAES_256");
        pbeByteEncryptor.setIvGenerator(new org.jasypt.iv.RandomIvGenerator());
    }

    public JasyptLocalTest() {
        basicTextEncryptor = new SimplePBEStringEncryptor(pbeByteEncryptor);
    }

    /**
     * 明文加密
     *
     * @param plaintext 明文
     * @return LoginTypeEnum
     */
    private String encrypt(String plaintext) {
        // 使用的加密算法参考2.2节内容，也可以在源码的类注释中看到
        return basicTextEncryptor.encrypt(plaintext);
    }

    /**
     * 解密
     *
     * @param ciphertext 密文
     * @return LoginTypeEnum
     */
    private String decrypt(String ciphertext) {
        ciphertext = "ENC(" + ciphertext + ")";
        if (PropertyValueEncryptionUtils.isEncryptedValue(ciphertext)) {
            return PropertyValueEncryptionUtils.decrypt(ciphertext, basicTextEncryptor);
        }
        return "";
    }
}
